<template>
	<view class="container">
		<view class="c-p-header color flex justify-center align-center">
			<view class="text-17">
				¥
			</view>
			<view class="text-23">
				{{parseFloat(realAmount/100).toFixed(2)}}
			</view>
		</view>
		<view class="margin-sm padding bg-white radius10">
			<view class="text-15 text-333">
				支付详情
			</view>
			<view class="flex justify-between align-center text-13 margin-top">
				<view class="text-999">
					应付金额
				</view>
				<view class="color">
					¥{{parseFloat(realAmount/100).toFixed(2)}}
				</view>
			</view>

			<view class="flex justify-between align-center text-13 margin-top">
				<view class="text-999">
					支付方式
				</view>
				<view class="text-333">
					{{payType=='A'?'微信支付':'账户余额支付'}}
				</view>
			</view>
		</view>
		<button @click="submit" class="bg-to-right round padding text-white text-15">立即支付</button>
		<passkeyborad :money='realAmount' @message='message' @close='close' :show="show"></passkeyborad>
	</view>
</template>

<script>
	import passkeyborad from '@/components/yzc-paykeyboard/yzc-paykeyboard.vue'
	export default {
		components: {
			passkeyborad
		},
		data() {
			return {
				orderId: '',
				payType: '',
				realAmount: 0,
				promoteAmount: '',
				show: false,
			};
		},
		onLoad(options) {
			console.log(options)
			if (options.orderId) this.orderId = options.orderId
			if (options.payType) this.payType = options.payType
			if (options.realAmount) this.realAmount =Number(options.realAmount) 
			if (options.promoteAmount) this.promoteAmount = options.promoteAmount
		},
		// onUnload() {
		// 	uni.setStorageSync('isBack', true)
		// 	uni.redirectTo({
		// 		url: "/filePage/pages/service-purchase/service-purchase"
		// 	})
		// },
		methods: {
			close() {
				// console.log(111111)
				this.show = false
			},
			message(password) {
				console.log(password)
				var CusBase64 = require('@/common/js/base64.js');
				//函数调用
				var passWord = CusBase64.CusBASE64.encoder(password);
				this.$get(this.$api.verifyPayPassWord, {
					passWord,
					userId: uni.getStorageSync('userId')
				}).then(res => {
					const [err, rs] = res
					// console.log('rs' + rs)
					// console.log( err.message=='请去设置密码！')
					if (rs) {
						const data = {
							orderId: this.orderId
						};
						this.$post(this.$api.order_accountPay, data).then(result => {
							const [err, rs] = result

							if (rs) {
								this.$util.msg(rs.message)
								this.show = false
								setTimeout(() => {
									uni.navigateTo({
										url: '/filePage/pages/service-purchase/service-purchase'
									})
								}, 1500)

							}

						})

					}
					if (err) {
						this.show = false
						if (err.message == '请去设置密码！') {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pagesA/pages/setPassword/setPassword'
								})
							}, 1500)
						}
					}
				})

			},
			async submit() {
				const data = {
					orderId: this.orderId
				};
				uni.showLoading({
					title: '正在支付',
					mask: true
				});
				if (this.payType == "A") {
					const [error, rs] = await this.$post(this.$api.order_wxPay, data);
					if (rs) {
						console.log(rs);
						uni.hideLoading();
						const data = rs.data;
						uni.requestPayment({
							provider: "wxpay",
							appId: data.appId,
							timeStamp: data.timeStamp,
							nonceStr: data.nonceStr,
							package: data._package,
							signType: data.signType,
							paySign: data.paySign,
							success: function(res) {
								console.log("success:" + JSON.stringify(res));
								setTimeout(() => {
									uni.navigateTo({
										url: '/filePage/pages/service-purchase/service-purchase'
									})
								}, 1500)

							},
							fail: function(err) {
								console.log("fail:" + JSON.stringify(err));
							},
						});
					}
				} else {
					uni.hideLoading();
					this.show = true
					// const [error, res] = await this.$post(this.$api.order_accountPay, data);
					// if (res) {
					// 	// console.log(res);
					// 	this.$util.msg(res.message)
					// 	uni.hideLoading();
					// 	setTimeout(() => {
					// 		uni.navigateTo({
					// 			url:'/filePage/pages/service-purchase/service-purchase'
					// 		})
					// 	}, 1500)
					// }
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.c-p-header {
		height: 156rpx;
	}

	button {
		margin: 128rpx 20rpx;
	}
</style>
